﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Common
{
    public class UnicomSMS
    {
        private static string requestPost(string postData, string uriStr)
        {

            HttpWebRequest requestScore = (HttpWebRequest)WebRequest.Create(uriStr);
            //postData = 
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] data = Encoding.GetEncoding("GBK").GetBytes(postData);
            requestScore.Method = "Post";
            requestScore.ContentType = "application/x-www-form-urlencoded";
            requestScore.ContentLength = data.Length;
            requestScore.KeepAlive = true;

            //使用cookies
            //requestScore.CookieContainer = ...;
            Stream stream = requestScore.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            HttpWebResponse responseSorce = (HttpWebResponse)requestScore.GetResponse();
            StreamReader reader = new StreamReader(responseSorce.GetResponseStream(), Encoding.GetEncoding("GBK"));
            string content = reader.ReadToEnd();

            requestScore = null;
            responseSorce.Close();
            responseSorce = null;
            reader = null;
            stream = null;

            return content;
        }

         
        public static string SendSMS(string phoneNum, string context)
        {
            string serialNumber = string.Empty;
            Random random = new Random();

            for (int i = 0; i < 20; i++)
            {
                serialNumber += random.Next(10);
            }
            serialNumber = serialNumber.PadLeft(20, '0');
            string postData = "SpCode=000697&LoginName=fanlv0&Password=a123456789&MessageContent=" + context +
           "&UserNumber=" + phoneNum + "&SerialNumber=" + serialNumber + "&ScheduleTime=&f=1";
            string uriStr = " http://www.ums86.com:8899/sms/Api/Send.do";
            return requestPost(postData, uriStr);
        }

    }
}
